package leetcode.easy;

import leetcode.base.TreeNode;

public class $530_GetMinimumDifference {

    int preVal;

    int ans;

    public int getMinimumDifference(TreeNode root) {
        // 二叉搜索树的最小绝对差 中序遍历即可
        ans = Integer.MAX_VALUE;
        preVal = -1;
        dfs(root);
        return ans;
    }

    private void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        dfs(root.left);

        if (preVal != -1) {
            ans = Math.min(ans, root.val - preVal);
        }
        preVal = root.val;

        dfs(root.right);
    }
}
